package com.xdf.xzymanagementsystem.service.base;

import com.xdf.xzymanagementsystem.entity.SysConfigInt;
import com.xdf.xzymanagementsystem.entity.SysUser;
import com.xdf.xzymanagementsystem.enums.ConfigIntEnum;
import com.xdf.xzymanagementsystem.service.SysConfigIntService;
import com.xdf.xzymanagementsystem.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @program: xzy-management-system
 * @description: 业务层基类
 * @author: 张柯
 * @create: 2021-01-05 15:12
 **/
@Component
public class BaseService {
    @Autowired
    private SysConfigIntService sysConfigIntService;
    @Autowired
    private SysUserService sysUserService;

    /**
     * 获取当前用户编号
     *
     * @return
     */
    protected Integer getLoginUserId() {
        return Integer.parseInt(String.valueOf(SecurityUtils.getSubject().getPrincipal()));
    }

    /**
     * 是否有某角色权限
     *
     * @param roleName
     * @return
     */
    protected boolean hasRole(String roleName) {
        return SecurityUtils.getSubject().hasRole(roleName);
    }

    /**
     * 是否有某权限
     *
     * @param permission
     * @return
     */
    protected boolean hasPermission(String permission) {
        return SecurityUtils.getSubject().isPermitted(permission);
    }

    /**
     * 是否拥有权限
     *
     * @param permission
     * @return
     */
    protected boolean hasPermission(String... permission) {
        return SecurityUtils.getSubject().isPermittedAll(permission);
    }

    /**
     * 获取请求中的数组
     *
     * @param request
     * @return
     */
    protected Map<String, Integer> conventToMap(HttpServletRequest request) {
        Enumeration<String> keys = request.getParameterNames();
        Map<String, Integer> map = new HashMap<>();
        while (keys.hasMoreElements()) {
            String name = keys.nextElement();
            String value = request.getParameter(name);
            Integer v = null;
            try {
                v = Integer.parseInt(value);
            } catch (Exception e) {
                continue;
            }
            if (v != null)
                map.put(name, v);
        }
        return map;
    }

    /**
     * 是否Excel
     *
     * @param fileName
     * @return
     */
    protected boolean isExcel(String fileName) {
        return !StringUtils.isEmpty(fileName) && (fileName.contains(".xls") || fileName.contains(".xlsx"));
    }

    /**
     * 查询当前配置
     *
     * @param config
     * @return
     */
    protected int getConfigIntValue(ConfigIntEnum config) throws Exception {
        Assert.notNull(config, "config为必输项！");
        String key = config.getField();
        return Optional.ofNullable(sysConfigIntService.selectByPrimaryKey(key)).map(SysConfigInt::getConfigValue).orElse(0);
    }

    /**
     * 校验日期
     *
     * @param orderDate
     * @throws Exception
     */
    protected void validateOrderDate(Date orderDate) throws Exception {
        int days = getConfigIntValue(ConfigIntEnum.ORDER_SAVE_INTERVAL);
        Date earliest = DateUtils.addDays(new Date(), -days);
        Date lastest = DateUtils.addDays(new Date(), days);
        Assert.state(earliest.compareTo(orderDate) <= 0 && lastest.compareTo(orderDate) >= 0, "单据日期必须与当前前后相差最多" + days + "天");
    }

    /**
     * 2021年8月28日 09:16:38 chanchaw
     * 获取当前登录用户的信息
     * @return
     */
    protected SysUser getLoginUserInfo() throws Exception {
        Integer userId = getLoginUserId();
        SysUser sysUser = sysUserService.selectByPrimaryKey(userId);
        return sysUser;
    }

    /**
     * 2021年8月28日 09:16:29  chanchaw
     * 获取当前登录系统的用户名
     * @return
     */
    protected String getLoginUsername() throws Exception {
        SysUser sysUser = getLoginUserInfo();
        return sysUser.getUserName();
    }
}
